/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-15 11:41:39
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-15 15:26:06
 * @FilePath: \gogamewebserver\goGameWebServer\internal\controller\cdkey\cdkey_v1.go
 * @Description: 文件功能描述
 */
package cdkey

import (
	"context"
	v1 "goGameWebServer/api/cdkey/v1"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *CdKeyControllerV1) QueryGroup(ctx context.Context, req *v1.CdKeyGroupQueryReq) (
	res *v1.CdKeyGroupQueryRes, err error) {
	gList, e := entity.GetAllCdKeyGroup()
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.CDKEY_GREOUP_QUERY_ERROR,
			cerror.GetErrMsg(cerror.CDKEY_GREOUP_QUERY_ERROR))
		return
	}
	controller.BackResponseOK(g.RequestFromCtx(ctx), gList)
	return
}

func (c *CdKeyControllerV1) UpdateGroup(ctx context.Context, req *v1.CdKeyGroupUpdateReq) (
	res *v1.CdKeyGroupUpdateRes, err error) {
	cdkeyModel := register.GetModel(entityName.EntityName_CdKeyGroup)
	cdKeyData := cdkeyModel.GetEntityData().(*entity.CdKeyGroupModelData)
	if req.Id != 0 {
		cdKeyData.Id = req.Id
		cdkeyModel.LoadEntityDataFromDB(nil)
		cdKeyData.Name = req.Name
		cdKeyData.Desc = req.Desc
		cdKeyData.Extra = req.Extra
		cdKeyData.Status = req.Status
		cdkeyModel.SaveEntityDataToDB()
	} else {
		cdKeyData.Name = req.Name
		cdKeyData.Desc = req.Desc
		cdKeyData.Extra = req.Extra
		cdKeyData.Status = req.Status
		cdkeyModel.Create()
	}
	res = &v1.CdKeyGroupUpdateRes{}
	res.Id = cdKeyData.Id
	res.Name = cdKeyData.Name
	res.Desc = cdKeyData.Desc
	res.Extra = cdKeyData.Extra
	res.Status = cdKeyData.Status
	res.CreateTime = cdKeyData.CreateTime
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *CdKeyControllerV1) Add(ctx context.Context, req *v1.CdKeyAddReq) (
	res *v1.CdKeyAddRes, err error) {
	// 查询code是否已经存在
	isExsit := entity.CodeIsExist(req.Code)
	if isExsit {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.CDKEY_HAS_EXIST_ERROR,
			cerror.GetErrMsg(cerror.CDKEY_HAS_EXIST_ERROR))
		return
	}
	cdkeyModel := register.GetModel(entityName.EntityName_CdKey)
	cdKeyData := cdkeyModel.GetEntityData().(*entity.CdKeyModelData)
	cdKeyData.GroupId = req.GroupId
	cdKeyData.Code = req.Code
	cdKeyData.LimitTime = req.LimitTime
	cdKeyData.CanUseCount = req.CanUseCount
	cdkeyModel.Create()
	res = &v1.CdKeyAddRes{}
	res.Id = cdKeyData.Id
	res.Code = cdKeyData.Code
	res.LimitTime = cdKeyData.LimitTime
	res.CanUseCount = cdKeyData.CanUseCount
	res.UsedCount = cdKeyData.UsedCount
	res.CreateTime = cdKeyData.CreateTime
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *CdKeyControllerV1) Query(ctx context.Context, req *v1.CdKeyQueryReq) (
	res *v1.CdKeyQueryRes, err error) {
	if req.Page < 1 {
		req.Page = 1
	}
	cdkes, e := entity.CdkeyQueryByGroupId(req.GroupId, req.Page, req.PageSize)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.CDKEY_QUERY_ERROR,
			cerror.GetErrMsg(cerror.CDKEY_QUERY_ERROR))
		return
	}
	controller.BackResponseOK(g.RequestFromCtx(ctx), cdkes)
	return
}
